<?php
namespace app\common\netfilter;

class Windows2012Rule
{

    public $rulehead = '';//规则头
    private $status; //状态
    private $protocols;//协议类型
    private $direction; //流量方向
    /**
     * @name: 状态
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-14 13:12:32
     */
    function status ($status)
    {
        $this->status = $status;
        $return = $status == 1 ? 'New' : 'remove';
        $return .= '-NetFirewallRule';
        
        return $return;
    }

    /**
     * @name: 规则名
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-14 13:18:34
     */
    function alias ($alias)
    {
        $return = '-DisplayName "' . $alias . '"';

        return $return;
    }

    /**
     * @name: 流量方向
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-14 13:19:46
     */
    function direction ($direction)
    {
        $this->direction = $direction;
        $return = '';
        if ($this->status != 1)
        {
            return $return;
        }
        $return =  '-Direction ';
        $return .= $direction == 1 ? 'Outbound' : 'Inbound';

        return $return;
    }

    /**
     * @name: 处理方式
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-14 13:20:35
     */
    function process ($process)
    {
        $return = '';
        if ($this->status != 1)
        {
            return $return;
        }

        $return = '-Action ';
        $return .= $process == 1 ? 'allow' : 'block';

        return $return;
    }

    /**
     * @name: ip地址
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-13 17:52:44
     */
    function ip ($src_ip, $dest_ip)
    {
        $return = '';

        if ($this->status != 1)
        {
            return $return;
        }


        if($this->direction == 1)
        {
            $remoteip = $dest_ip;
            $localip = $src_ip;
        }
        else
        {
            $remoteip = $src_ip;
            $localip = $dest_ip;
        }
        

        $return .= $localip == 'any' ? '' : '-LocalAddress ' . $localip . ' ';
        $return .= $remoteip == 'any' ? '' : '-RemoteAddress ' . $remoteip;

        return $return;
    }

    /**
     * @name: 协议
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-14 13:27:53
     */
    function protocols ($protocols)
    {
        $this->protocols = $protocols;
        $return = '';
        if ($protocols == 3 || $this->status != 1)
        {
            return $return;
        }

        $return = '-Protocol ';
        $return .= $protocols == 1 ? 'TCP' : 'UDP';
        return $return;
    }
    /**
     * @name: 端口
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-14 13:24:03
     */
    function port ($src_port, $dest_port)
    {
        $return = '';

        if ($this->status != 1 || $this->protocols == 3)
        {
            return $return;
        }
        $dest_port = str_replace(':', '-', $dest_port);
        $src_port = str_replace(':', '-', $src_port);

        if ($this->direction == 1)
        {
            $remoteport = $dest_port;
            $localport = $src_port;
        }
        else
        {
            $remoteport = $src_port;
            $localport = $dest_port;
        }
        
        $return .= $remoteport == 'any' ? '' : '-RemotePort ' . $remoteport . ' ';
        $return .= $localport == 'any' ? '' : '-LocalPort ' . $localport;

        return $return;
    }
}